﻿using System;
using System.Linq;
using System.Collections.Generic;

using Infrastructure.NetExtensions;
using TempEmployees.DAL;
using TempEmployees.BLL.Database;
using TempEmployees.BLL.EntitiesExtensions;

namespace TempEmployees.Web
{
    public partial class WebForm4 : ABasePage
    {
        private static readonly string BUTTON_UPDATE_ASSIGNMENT = WebConfigData.BUTTON_UPDATE_ASSIGNMENT;

        private int AssignmentId
        {
            set { ViewState.Set<int>("AssignmentId", value); }
            get { return ViewState.Get<int>("AssignmentId"); }
        }

        private string SourcePage
        {
            set { ViewState.Set<string>("SourcePage", value); }
            get { return ViewState.Get<string>("SourcePage"); }
        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            //page defualt button
            Page.Form.DefaultButton = ButtonUpdateAssignment.UniqueID;

            if (!IsPostBack)
            {
                GetPageData();
                SetDepartmentStatusGridView();
                SetAssignmentStatusGridView();
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //assignment details
                AssignmentDetails1.FillAssignmentDetails(AssignmentId);
                AssignmentDetails1.IsReadOnly = true;
                //employee details
                SetEmployeeDetails();
                SetButtons();
            }
        }

        private void GetPageData()
        {
            AssignmentDetailsPageData data = APageData.Get<AssignmentDetailsPageData>();
            //Session.Remove(GUIServices.DATA_DETAILS_ASSIGNMENT);
            AssignmentId = data.AssignmentId;
            SourcePage = data.SourcePage;
        }

        protected void LinkButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(SourcePage);
        }

        protected void ButtonUpdateAssignment_Click(object sender, EventArgs e)
        {
            if (AssignmentDetails1.IsReadOnly)
            {
                AssignmentDetails1.IsReadOnly = false;
                ButtonUpdateAssignment.Text = BUTTON_UPDATE_ASSIGNMENT;
                return;
            }

            if (!AssignmentDetails1.UpdateAssignment(AssignmentId)) return;

            AfterAssignmentUpdated(UpdatedAssignmentData.eUpdateType.Update);
        }

        private void SetButtons()
        {
            Assignment assignment = EAssignment.GetAssignmentById(AssignmentId);
            DbConstants.Status status = (DbConstants.Status)assignment.StatusID;
            DbConstants.Roles role = State.UserRole;

            //update assignment button
            if (status != DbConstants.Status.Finished &&
                (role == DbConstants.Roles.Secretary || role == DbConstants.Roles.Employer || role == DbConstants.Roles.HeadDepartment)) 
                ButtonUpdateAssignment.Visible = true;

            //update status buttons
            switch (role)
            {
                case DbConstants.Roles.Secretary:
                    if (status == DbConstants.Status.Approved) ButtonActivate.Visible = true;
                    break;
                case DbConstants.Roles.HeadDepartment:
                    if (status == DbConstants.Status.Pending)
                    {
                        ButtonAccept.Visible = true;
                        ButtonReject.Visible = true;
                    }
                    break;
            }

            //extend assignment button
            if (status == DbConstants.Status.Active) ButtonExtend.Visible = true;

            //terminate status button
            /* for now, don't need this button at all
            if ((role == DbConstants.Roles.Employer || role == DbConstants.Roles.Secretary) &&
                (status == DbConstants.Status.Active || status == DbConstants.Status.Approved || status == DbConstants.Status.Pending))
                ButtonTerminate.Visible = true;
            */ 
            ButtonTerminate.Visible = false;
        }

        private void SetEmployeeDetails()
        {
            string employeeId = DbServices.db.Assignments.First(a => a.ID == AssignmentId).Employee.ID;
            EmployeeDetails1.SetEmployeeID(employeeId);
            EmployeeDetails1.ReadonlyControls(true);
        }

        protected void ButtonEmployeeDetails_Click(object sender, EventArgs e)
        {
            string employeeId = DbServices.db.Assignments.First(a => a.ID == AssignmentId).Employee.ID;
            APageData.Set(new EmployeeDetailsPageData(employeeId, GUIServices.PAGE_UPDATE_ASSIGNMENT));

            Response.Redirect(GUIServices.PAGE_UPDATE_EMPLOYEE);
        }

        private void AfterAssignmentUpdated(UpdatedAssignmentData.eUpdateType updateType)
        {
            APageData.Set(new UpdatedAssignmentData(AssignmentId, updateType));

            Response.Redirect(GUIServices.PAGE_SEARCH_ASSIGNMENTS);
        }
 
        private void UpdateStatus(DbConstants.Status status)
        {
            Assignment assignment = EAssignment.GetAssignmentById(AssignmentId);
            assignment.UpdateStatus(status);
            switch (status)
            {
                case DbConstants.Status.Active:
                    AfterAssignmentUpdated(UpdatedAssignmentData.eUpdateType.Activated);
                    break;
                case DbConstants.Status.Approved:
                    AfterAssignmentUpdated(UpdatedAssignmentData.eUpdateType.Approved);
                    break;
                case DbConstants.Status.Rejected:
                    AfterAssignmentUpdated(UpdatedAssignmentData.eUpdateType.Rejected);
                    break;
            }
        }
        protected void ButtonExtend_Click(object sender, EventArgs e)
        {
            APageData.Set(new ExtendAssignmentData(AssignmentId));
            Response.Redirect(GUIServices.PAGE_NEW_ASSIGNMENT);
        }
        protected void ButtonActivate_Click(object sender, EventArgs e)
        {
            UpdateStatus(DbConstants.Status.Active);
        }
        protected void ButtonAccept_Click(object sender, EventArgs e)
        {
            if (!AssignmentDetails1.IsReadOnly && !AssignmentDetails1.UpdateAssignment(AssignmentId)) return;
            UpdateStatus(DbConstants.Status.Approved);
        }
        protected void ButtonReject_Click(object sender, EventArgs e)
        {
            UpdateStatus(DbConstants.Status.Rejected);
        }
        protected void ButtonTerminate_Click(object sender, EventArgs e)
        {
            UpdateStatus(DbConstants.Status.Finished);
        }

        private void SetDepartmentStatusGridView()
        {
            Filters filters = new Filters();
            filters.Add("SemesterID", 1);
            filters.Add("DepartmentID", 1);
            DepartmentStatusGridView.SetGridView(filters);
        }

        private void SetAssignmentStatusGridView()
        {
            List<object> statusList = new List<object>();
            statusList.Add((int)DbConstants.Status.Active);
            statusList.Add((int)DbConstants.Status.Approved);

            Filters filters = new Filters();
            filters.Add("DepartmentID", 1);
            filters.Add("SemesterID", 1);
            filters.Add("StatusID", statusList);

            AssignmentStatusGridView.SetGridView(filters);
        }
    }

    public class AssignmentDetailsPageData : APageData
    {
        private int m_assignmentId;
        private string m_sourcePage;

        public int AssignmentId { get { return m_assignmentId; } }
        public string SourcePage { get { return m_sourcePage; } }

        public AssignmentDetailsPageData(int assignmentId, string sourcePage)
        {
            m_assignmentId = assignmentId;
            m_sourcePage = sourcePage;
        }
    }
}
